﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core;

namespace Microhard.WeixinMP.Impls
{
    public class ImageImpl : IImage
    {
        public Core.DBUtility.ExecutedResult Add(Models.ImageModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            Image op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.Images.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("ImageImpl.Add", "接收图片添加失败");
                Log.Error("ImageImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ImageModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var toDeltes = from b in db.Images
                           where ids.Contains(b.PicID)
                           select b;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("ImageImpl.Delete", "接收图片删除失败");
                Log.Error("ImageImpl.Delete", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ImageModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ImageModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.Images.SingleOrDefault(m => m.PicID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Models.ImageModel GetModelByModel(Models.ImageModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ImageModel> Query(Models.ImageModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.ImageModel> Query(Models.ImageModel Model)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            IEnumerable<Image> entities = from b in db.Images
                                          where b.ConversationID == Model.ConversationID
                                          select b;
            return entities.Select(m => m.ToModel()).ToList();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
